package com.example.demo.controller;

import com.example.demo.entity.ReturnStatus;
import com.example.demo.entity.UserBasicInfo;
import com.example.demo.entity.UserCommonInfo;
import com.example.demo.entity.UserFeedbackInfo;
import com.example.demo.mapper.UserCommonInfoMapper;
import com.example.demo.mapper.UserFeedbackInfoMapper;
import com.example.demo.mapper.UserMapper;
import lombok.Data;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/userapi")
public class UserInfoControllor {
    @Data
    static class CommonInfo{
        String my_id;
        String my_name;
        String my_phone;
    }
    @Data
    static class SignIn{
        String phone;
        String password;
    }
    @Resource
    private UserMapper userMapper;
    @PostMapping("/login")
    public ReturnStatus<UserBasicInfo> login(@RequestBody SignIn signJson){
        if (signJson.phone ==null || signJson.password == null) {
            System.out.println("用户名或密码为空！");
        }
        UserBasicInfo user = new UserBasicInfo();
        user = userMapper.find_my_user_basic_info(signJson.phone);
        if(user == null) {
            return new ReturnStatus<UserBasicInfo>(0,null);
        }
        return new ReturnStatus<UserBasicInfo>(1,user);
    }
    //    @PostMapping("/singup")
//    public String signUp(@RequestBody Controller.SignUp signUpJson) {
//        return "OK";
//    }
    @Resource
    private UserFeedbackInfoMapper userFeedbackInfoMapper;
    @PostMapping("/insert")
    public String upData(@RequestBody UserFeedbackInfo feedbackInfo) {
        feedbackInfo.setFeedback_time(new Date());
        feedbackInfo.setIs_viewed(0);
        int result = userFeedbackInfoMapper.insert_my_user_feedback_info(feedbackInfo);
        if (result == 1) {
            return "1";
        }
        return "0";
    }

    @Resource
    private UserCommonInfoMapper userCommonInfoMapper;

    @PostMapping("/delete_address")
    public String delete_address(@RequestBody CommonInfo userInfo) {
        UserCommonInfo user = new UserCommonInfo();
        user = userCommonInfoMapper.select_user_common_address_info(userInfo.my_id,userInfo.my_name,userInfo.my_phone);
        if (user == null) {
            System.out.println("找不到该用户的相关信息！");
            return "0";
        }
        if (userCommonInfoMapper.delete_user_common_address_info(userInfo.my_id,userInfo.my_name,userInfo.my_phone) == 1) {
            return "1";
        }
        return "0";
    }
    @GetMapping("/test")
    public String test(){
        return "success";
    }
}
